package com.itheima.advantagesup.mapper;

import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.entity.SysRole;
import com.itheima.advantagesup.entity.SysRoleInfo;
import com.itheima.advantagesup.vo.RoleAllDataVO;
import com.itheima.advantagesup.vo.RoleVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * SysRoleMapper
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/26 17:51
 */
@Mapper
public interface SysRoleMapper {
    /**
     * 选择名单由ids
     *
     * @param roleIds 角色id
     * @return {@link List}<{@link String}>
     */
    List<String> selectNameListByIds(List<String> roleIds);

    /**
     * 根据名字模糊查询
     * @param name
     * @return
     */
    List<RoleVO> selectList(String name);

    /**
     * 根据role_id查询sys_role_menu表中的 menu_id 字段
     * @param role_id
     * @return
     */
    @Select("select menu_id from sys_role_menu where role_id = #{role_id}")
    List<Integer> selectMenuIdByRoleId(String role_id);


    /**
     * 往 sys_role 表添加数据
     * @param sysRole
     */
    void insert(SysRole sysRole);

    /**
     * 往 sys_role_menu 表添加数据
     * @param roleId
     * @param menuId
     */
    @Insert("insert into sys_role_menu(role_id, menu_id) values (#{roleId},#{menuId})")
    void insertRoleMenu(String roleId, Integer menuId);

    /**
     * 根据id查询对应的角色(一个)
     * @param id
     * @return {@link SysRole}
     */
    @Select("select * from sys_role where id = #{id}")
    SysRole selectRoleById(String id);

    /**
     * 回显
     * @param roleId
     * @return {@link List}<{@link Integer}>
     */
    @Select("select menu_id from sys_role_menu where role_id = #{roleId} ")
    List<Integer> selectMenuIDsByRoleID(String roleId);

    @Select("select * from sys_menu where pid = #{pid}")
    List<SysMenu> getAllDataByPId(Integer pid);

    /**
     * 修改sys_role表
     * @param sysRole
     */
    void update(SysRole sysRole);


    /**
     * 统计menu_id存在的数量
     * @param menuId
     * @return int
     */
    @Select("select COUNT(*) from sys_role_menu where menu_id = #{menuId}")
    Integer selectCountByMenuId(Integer menuId);

    @Delete("delete from sys_role_menu where role_id = #{roleId} and menu_id = #{menuId}")
    void deleteRoleMenu(String roleId, Integer menuId);

    /**
     * 根据id删除role表的数据
     * @param roleID
     */
    @Delete("delete from sys_role where id = #{roleID}")
    void deleteRoleById(String roleID);

    /**
     * 根据角色id删除角色菜单表的数据
     * @param roleId
     */
    @Delete("delete from sys_role_menu where role_id = #{roleId}")
    void deleteRoleMenuById(String roleId);

    @Select("select id, name, description, create_date, update_date, remarks, del_flag, superadmin, default_data from sys_role")
    List<RoleAllDataVO> selectRole();

    void insertRoleMenu2(String roleId,List<Integer> menuids);

    @Select("select * from sys_role where id = #{roleId}")
    SysRoleInfo selectRoleInfoById(String roleId);

    List<SysMenu> selectListByRoleId(String roleId);

    //void insertRoleMenus(String roleId, List<Integer> menuids);


    /*void updateRoleMenuByRoleId(SysRole sysRole);*/

    /*@Update("update sys_role_menu set menu_id = #{menuId} where role_id = #{roleId}")
    void updataRoleMenu(String roleId, int menuId);*/
}
